<?php
    $this->pageTitle=Yii::t('site', 'Stadium Timeslot');

    $cs = Yii::app()->getClientScript();
    $cs->registerCSSFile(Yii::app()->request->baseUrl.'/css/defaultTheme.css');
    $cs->registerScriptFile(Yii::app()->request->baseUrl.'/js/jquery.fixedheadertable.min.js');
?>
<div class="form">
<?php
    $buttonDays = 3;
    $startDate = $startDate == NULL ? strtotime($date) : strtotime($startDate);
    if ($buttonDays == 7 && date("N", $startDate) != 1) {
        $startDate = strtotime('last monday', $startDate);
    }

    $selectedDate = new DateTime($date);
    $currDate = new DateTime(date("Y-m-d", $startDate));
    
    $prevWeek = clone $currDate;
    $prevWeek = $prevWeek->sub(new DateInterval('P'.$buttonDays.'D'));
    
    $nextWeek = clone $currDate;
    $nextWeek = $nextWeek->add(new DateInterval('P'.$buttonDays.'D'));
?>
    <div class="row">
		<?php echo $model->name; ?>
        <form method="get" action="<?php echo Yii::app()->createUrl('stadium/timeslot'); ?>">
        <input type="hidden" name="startDate" value="<?php echo date("Y-m-d", $startDate); ?>" />
        <?php
            foreach ($_GET as $key => $value) {
                if ($key != "date" && $key != "startDate") {
                    echo("<input type='hidden' name='$key' value='$value'/>");
                }
            }
        ?>
        <fieldset data-role="controlgroup" data-type="horizontal">
            <input type="radio" onchange="changeDate(this.form)" name="date" id="d0" value="<?php echo $prevWeek->format("Y-m-d"); ?>">
            <label for="d0"> &lt;<br/>&lt; </label>
            <?php
                for ($i = 1; $i <= $buttonDays; $i++) {
                    $checked = "";
                    if (Controller::isSameDay($currDate, $selectedDate)) {
                        $checked = ' checked="checked"';
                    }
            ?>
            <input type="radio" onchange="this.form.submit()" name="date" id="d<?php echo $i; ?>" value="<?php echo $currDate->format("Y-m-d"); ?>"<?php echo $checked; ?>>
            <label for="d<?php echo $i; ?>"><?php echo $currDate->format("m-d"); ?><br/><?php echo $currDate->format("D"); ?></label>
            <?php
                    $currDate->add(new DateInterval('P1D'));
                }
            ?>
            <input type="radio" onchange="changeDate(this.form)" name="date" id="d<?php echo $i; ?>" value="<?php echo $nextWeek->format("Y-m-d"); ?>">
            <label for="d<?php echo $i; ?>"> &gt;<br/>&gt; </label>
        </fieldset>
        </form>

        <form method="post" onsubmit="return submitIt(this)" action="<?php echo Yii::app()->createUrl('stadium/booking'); ?>">
        <input type="hidden" name="date" value="<?php echo $selectedDate->format('Y-m-d'); ?>" />
        <input type="hidden" name="bookings" />
        <button class="ui-btn ui-btn-inline"><?php echo Yii::t('site', 'Booking'); ?></button>
        </form>
    </div>

    <div id='tableDiv' style="width:100%;">
    <table class="fancyTable" id="myTable01">
        <thead>
        <tr>
            <th>T<br/>Courts</th>
            <?php
                $currDate = clone $slotInfo['min'];
                for ($i=1; $i<=$slotInfo['count']; $i++) {
            ?>
            <th><?php
                    echo '&nbsp;'.$currDate->format('H:i').'&nbsp;';
                    $currDate->add(new DateInterval('PT1H'));
                ?></th>
            <?php } ?>
        </tr>
        </thead>
        <tbody>
        <?php
            foreach ($resources as $id=>$resource) {
                $bookings = Booking::model()->getList($resource->id, $selectedDate);
                $currDate = clone $slotInfo['min'];
        ?>
        <tr>
            <td align="center" style="vertical-align:middle;"><?php echo $resource->name; ?></td>
            <?php for ($i=1; $i<=$slotInfo['count']; $i++) { ?>
            <td align="center" style="vertical-align:middle;"><?php
                    $status = $this->getBookStatus($resource, $bookableResources, $bookings, $slotInfo, $currDate);
                    if ($status->isBooked()) { //booked
                        if ($status->isByMe()) {
                            echo '<b style="color:blue;" title="My Bookings">B</b>';
                        } else {
                            echo '<b>B</b>';
                        }
                    } else if ($status->isUnavailable()) { //unavailable
                        echo '<b>U</b>';
                    } else { ?>
                <div class="bookable-div" v="<?php echo $status->bookableResourceId . '|'. $currDate->format('H:i:s'); ?>"></div>
                    <?php }
                ?>
            </td>
            <?php
                    $currDate->add(new DateInterval('PT1H'));
                }
            ?>
        </tr>
        <?php } ?>
        </tbody>
    </table>
    </div>
    <div class="clear"></div>

</div><!-- form -->
<script>
$('#mainPage').on('pageshow', function () {
    var header = $.mobile.activePage.find("div[data-role='header']:visible");
    var footer = $.mobile.activePage.find("div[data-role='footer']:visible");
    var height = footer.offset().top - $('#tableDiv').offset().top - header.outerHeight();
    if (height < 150) {
        height = 150;
    }
    $("#tableDiv").height(height);
    $('#myTable01').fixedHeaderTable({ altClass: 'odd', footer: true, cloneHeadToFoot: true, fixedColumns: 1 });
    
    $('.bookable-div').click(function() {
        $(this).toggleClass('bookable-div-selected');
    });
    
    $('.fht-tbody').css("margin-top", 0); //patch for position
});

function submitIt(frmThis) {
    var bookings = "";
    $('.bookable-div-selected').each(function() {
        bookings += $(this).attr("v") + ";";
    });

    frmThis.bookings.value = bookings;
    if (frmThis.bookings.value == "") {
        alert("Please select time to bookings");
        return false;
    } else {
        return true;
    }
}

function changeDate(frmThis) {
    frmThis.startDate.value = "";
    frmThis.submit();
}
</script>